package cn.iocoder.yudao.module.dbit.service.majinfo;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import cn.iocoder.yudao.module.dbit.controller.admin.majinfo.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.majinfo.MajInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.dbit.dal.mysql.majinfo.MajInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dbit.enums.ErrorCodeConstants.*;

/**
 * 共建专业 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MajInfoServiceImpl implements MajInfoService {

    @Resource
    private MajInfoMapper majInfoMapper;

    @Override
    public Integer createMajInfo(MajInfoSaveReqVO createReqVO) {
        // 插入
        MajInfoDO majInfo = BeanUtils.toBean(createReqVO, MajInfoDO.class);
        majInfoMapper.insert(majInfo);
        // 返回
        return majInfo.getMajId();
    }

    @Override
    public void updateMajInfo(MajInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateMajInfoExists(updateReqVO.getMajId());
        // 更新
        MajInfoDO updateObj = BeanUtils.toBean(updateReqVO, MajInfoDO.class);
        majInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteMajInfo(Integer id) {
        // 校验存在
        validateMajInfoExists(id);
        // 删除
        majInfoMapper.deleteById(id);
    }

    private void validateMajInfoExists(Integer id) {
        if (majInfoMapper.selectById(id) == null) {
            throw exception(MAJ_INFO_NOT_EXISTS);
        }
    }

    @Override
    public MajInfoDO getMajInfo(Integer id) {
        return majInfoMapper.selectById(id);
    }

    @Override
    public PageResult<MajInfoDO> getMajInfoPage(MajInfoPageReqVO pageReqVO) {
        return majInfoMapper.selectPage(pageReqVO);
    }

}